package servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import bean.User;
import service.UserService;
import service.impl.UserServiceImpl;

@WebServlet("/user/register")
public class RegisterServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String mobile = request.getParameter("mobile");

        User user = new User();
        user.setName(username);
        user.setPassword(password);
        user.setMobile(mobile);

        if (userService.register(user)) {
            request.setAttribute("message","注册成功");
            request.setAttribute("page","login.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        } else {
            request.setAttribute("message","注册失败,该用户存在");
            request.setAttribute("page","register.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        }
    }
}
